package product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;



import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CodeUpdate extends JInternalFrame {

	DefaultTableModel model;
	Vector<String> col = new Vector<String>();
	HashMap<String, CodeData> cod;
	DecimalFormat df = new DecimalFormat("###,###,###.##");
	
	private JTextField findStr;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	/**
	 * Create the frame.
	 */
	public CodeUpdate(HashMap<String, CodeData> c) {
		super("CodeUpdate", true, true, true, false);
		this.cod = c;
		col.add("제품코드");
		col.add("제품명");
		col.add("단가");
		model = new DefaultTableModel(col, 0);
		setBounds(100, 100, 230, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getTextField_1());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());
		
		DefaultTableCellRenderer Center = new DefaultTableCellRenderer();
		Center.setHorizontalAlignment(SwingConstants.CENTER);//가운데 정렬
		DefaultTableCellRenderer Right = new DefaultTableCellRenderer();
		Right.setHorizontalAlignment(SwingConstants.RIGHT);//우측 정렬
		TableColumnModel tcmSchedule = table.getColumnModel();

		tcmSchedule.getColumn(0).setCellRenderer(Center);
		tcmSchedule.getColumn(1).setCellRenderer(Center);
		tcmSchedule.getColumn(2).setCellRenderer(Right);//단가
		
	}
	
	public String replace(String s) {
		String temp = s.replace(",","");
		String temp2 = temp.replace("원","");
		return temp2;
	}
	
	private JTextField getTextField_1() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(10, 10, 103, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (model.getRowCount() > 0) { // 행의 수를 읽어 온다.
						for (int i = model.getRowCount() - 1; i > -1; i--) {
							model.removeRow(i);
						}
					}					
					String find = findStr.getText();
					
					Set<String> st = cod.keySet();
					Iterator<String> it = st.iterator();
					
					if (("").equals(find)) {
						while (it.hasNext()) {
							Vector<String> row = new Vector<String>();
							String key = it.next();
							CodeData value = cod.get(key);
							String str = df.format((double)value.getProPrice());
							row.add(value.getProCode());
							row.add(value.getProName());
							row.add(str+"원");

							model.addRow(row);
						}
					}else{
						while (it.hasNext()) {
							Vector<String> row = new Vector<String>();
							String key = it.next();
							CodeData value = cod.get(key);
							if (value.getProCode().contains(find)
									|| value.getProName().contains(find)) {
								row.add(value.getProCode());
								row.add(value.getProName());
								row.add(replace(String.valueOf(value.getProPrice())));

								model.addRow(row);
							}
						}
					}
				}
			});
			btnNewButton.setBounds(125, 10, 75, 21);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 46, 190, 215);
			scrollPane.setViewportView(getTable_1());
		}
		return scrollPane;
	}
	private JTable getTable_1() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					int index = table.getSelectedRow();
					Vector<String> vec = new Vector<String>();
					vec.add((String) table.getValueAt(index, 0));
					vec.add((String) table.getValueAt(index, 1));
					vec.add((String) table.getValueAt(index, 2));

					CodeTableAction cta = new CodeTableAction(cod,vec,model,index);
					cta.setVisible(true);
					
				}
			});
		}
		return table;
	}
}
